/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2019-12-28
 * Time: 21:18
 */
#include "UserDao.h"

bool UserDao::save(User user) {
    bool flag = false;
    DBHelper::conn();
    QSqlQuery query;
    QString sql = QString(
            "insert into plantsinfo.userdata(name, pwd, level, time) VALUES ('%1','%2','%3','%4');").arg(
            user.getName()).arg(user.getPasswd()).arg(user.getLevel()).arg(user.getSignTime());
    int success = query.exec(sql);
    if (!success) {
        flag = false;  //失败
    } else {
        flag = true;   //成功
    }
    return flag;
}

/**
     * 功能：检查用户名
     * @param user
     * @return
     */
bool UserDao::checkUserInfo(User user) {
    bool flag = false;
    DBHelper::conn();
    QSqlQuery query;
    QString sql = QString("select * from plantsinfo.userdata where name ='%1';").arg(user.getName());
    //qDebug() << sql;
    int success = query.exec(sql);
    if (!success) {
        flag = false;  //失败
    } else {
        while (query.next()) {
            if (query.value(1).toString() == user.getName()) {
                flag = true;
            }
        }
    }
    return flag;
}

/**
     * 功能：检查用户密码是否正确
     * @param user
     * @return
     */
bool UserDao::checkPasswdInfo(User user) {
    bool flag = false;
    DBHelper::conn();
    QSqlQuery query;
    QString sql = QString("select pwd from plantsinfo.userdata where name ='%1';").arg(user.getName());
    //qDebug()<<sql;
    int success = query.exec(sql);
    if (!success) {
        //失败
        flag = false;
    } else {
        //成功
        while (query.next()) {
            if (query.value(0).toString() == user.getPasswd()) {
                flag = true;
            }
        }
    }
    return flag;
}
